﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace cb0tServer
{
    class Greetings
    {
        private static List<String> messages;
        private static int pos;

        public static bool MessagesExist
        {
            get { return messages.Count > 0; }
        }

        public static String NextMessage
        {
            get
            {
                if (messages.Count == 0)
                    return null;

                if (pos >= messages.Count)
                    pos = 0;

                String result = messages[pos];
                pos++;
                return result;
            }
        }

        public static void AddMessage(String text)
        {
            messages.Add(text);
            Save();
        }

        public static bool RemMessage(int id)
        {
            if (id < 0 || id >= messages.Count)
                return false;

            messages.RemoveAt(id);
            Save();
            return true;
        }

        public static void ViewMessages(UserObject userobj)
        {
            userobj.SendOutboundPacket(ServerOutboundPackets.AnnoucePacket("\x0007\x000314GREET MESSAGE LIST"));
            userobj.SendOutboundPacket(ServerOutboundPackets.AnnoucePacket(""));

            if (messages.Count == 0)
            {
                userobj.SendOutboundPacket(ServerOutboundPackets.AnnoucePacket("\x000314No greet messages currently set"));
            }
            else
            {
                for (int i = 0; i < messages.Count; i++)
                    userobj.SendOutboundPacket(ServerOutboundPackets.AnnoucePacket("\x000314" + i + " - " + messages[i]));
            }

            userobj.SendOutboundPacket(ServerOutboundPackets.AnnoucePacket(""));
            userobj.SendOutboundPacket(ServerOutboundPackets.AnnoucePacket("\x0007\x000314List Complete"));
        }

        public static void Load()
        {
            messages = new List<String>();
            pos = 0;

            try
            {
                FileStream f = new FileStream(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "data/greets.xml", FileMode.Open);
                XmlReader xml = XmlReader.Create(new StreamReader(f));

                xml.MoveToContent();
                xml.ReadSubtree().ReadToFollowing("greetmessages");

                while (xml.ReadToFollowing("item"))
                {
                    xml.ReadSubtree().ReadToFollowing("text");
                    messages.Add(Encoding.UTF8.GetString(Convert.FromBase64String(xml.ReadElementContentAsString())));
                }

                xml.Close();
                f.Flush();
                f.Close();
            }
            catch { }
        }

        private static void Save()
        {
            try
            {
                XmlWriterSettings appearance = new XmlWriterSettings();
                appearance.Indent = true;
                XmlWriter xml = XmlWriter.Create(sb0t_winGUI.RunOnceFileUpdater.sbotpath + "data/greets.xml", appearance);

                xml.WriteStartDocument();
                xml.WriteStartElement("greetmessages");

                foreach (String a in messages)
                {
                    xml.WriteStartElement("item");
                    xml.WriteElementString("text", Convert.ToBase64String(Encoding.UTF8.GetBytes(a)));
                    xml.WriteEndElement();
                }

                xml.WriteEndElement();
                xml.WriteEndDocument();
                xml.Flush();
                xml.Close();
            }
            catch { }
        }
    }
}
